using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public class DataAccessUser
    {
        public static ICollection<User> GetUsersById(Model.User user)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.User
                    where u.UserId == user.UserId
                    select u).ToList();
        }

        public static ICollection<User> GetActiveUsersById(Model.User user)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.User
                    where u.UserId == user.UserId && u.IsActive == true
                    select u).ToList();
        }

        public static ICollection<User> GetUserByLogin(Model.User user)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.User
                    where u.Login== user.Login
                    select u).ToList();
        }

        public static void CreateUser(Model.User newUser)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.User.Add(newUser);
            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection<Model.User> SearchUserByParameter(string parameter)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where u.Login.Contains(parameter) ||
                     u.Person.LastNames.Contains(parameter) 

                    select u;
            return q.ToList();
        }

        public static ICollection<Model.User> GetUsersByLoginAndPassword(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Password.Equals(userToFind.Password) &&
                          u.Login.Equals(userToFind.Login) && u.IsActive)
                     select u;

            ICollection<Model.User> resultado = q.ToList();
            if (resultado.Count > 0)
            {
                userToFind.Role = q.First().Role;
                userToFind.UserId = q.First().UserId;
            }
            return resultado;
        }


        public static ICollection<Model.User> SearchUserByParameterEnable(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Person.Names.Contains(userToFind.Person.Names) &&
                          u.Person.LastNames.Contains(userToFind.Person.LastNames) &&
                          u.Login.Contains(userToFind.Login)) && (u.IsActive)
                    select u;
            return q.ToList();
        }

        public static ICollection<Model.User> SearchUserByParameterDisable(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Person.Names.Contains(userToFind.Person.Names) &&
                          u.Person.LastNames.Contains(userToFind.Person.LastNames) &&
                          u.Login.Contains(userToFind.Login) && (u.IsActive == false))
                    select u;
            return q.ToList();
        }

        public static ICollection<Model.User> SearchUserByParameterAll(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Person.Names.Contains(userToFind.Person.Names) &&
                          u.Person.LastNames.Contains(userToFind.Person.LastNames) &&
                          u.Login.Contains(userToFind.Login))
                    select u;
            return q.ToList();
        }

        public static void UpdateUser(Model.User modifyUser)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.User q = (from u in WFPEntities.Instance.User
                              where u.UserId == modifyUser.UserId
                              select u).First();
            q.Login = modifyUser.Login;
            q.Role = modifyUser.Role;
            WFPEntities.Instance.SaveChanges();
        }

        public static void UpdateUserPassword(Model.User modifyUser)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.User q = (from u in WFPEntities.Instance.User
                            where u.UserId == modifyUser.UserId
                            select u).First();
            q.Password = modifyUser.Password;
            WFPEntities.Instance.SaveChanges();
        }

        public static void DisableUser(Model.User UserForDisable)
        {
            try
            {
                Model.User q = (from u in WFPEntities.Instance.User
                                  where u.UserId == UserForDisable.UserId
                                  select u).First();
                q.IsActive = false;
                WFPEntities.Instance.SaveChanges();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForDisable.UserId
                                  select u).First();
                q.IsActive = false;
                WFPEntities.Instance.SaveChanges();
            }
        }

        public static void EnableUser(Model.User UserForEnable)
        {
            try
            {
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForEnable.UserId
                                select u).First();
                q.IsActive = true;
                WFPEntities.Instance.SaveChanges();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForEnable.UserId
                                select u).First();
                q.IsActive = true;
                WFPEntities.Instance.SaveChanges();
            }
        }
    }
}
